'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 4_study_del_message.py
* @Time: 2025/11/4
* @All Rights Reserve By Brtc
'''
import dotenv
from langchain_core.messages import RemoveMessage, AIMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState, StateGraph

dotenv.load_dotenv()
llm = ChatOpenAI(model="gpt-4o-mini")

def chat_bot(state:MessagesState)->MessagesState:
    """聊天机器人节点"""
    return {"messages":[llm.invoke(state["messages"])]}

def delete_human_message(state:MessagesState)->MessagesState:
    """删除人类消息"""
    human_message = state["messages"][0]
    return {"messages":[RemoveMessage(id=human_message.id)]}

def update_ai_message(state:MessagesState)->MessagesState:
    """修改AI 消息"""
    ai_message = state["messages"][-1]
    return {"messages":[AIMessage(id=ai_message.id, content=ai_message.content + "我被黄晓康修改过了！")]}


#1、创建图构建器
graph_builder = StateGraph(MessagesState)
#2、添加节点
graph_builder.add_node("chat_bot", chat_bot)
graph_builder.add_node("update_ai_message", update_ai_message)
#3、添加边
graph_builder.set_entry_point("chat_bot")
graph_builder.add_edge("chat_bot", "update_ai_message")
graph_builder.set_finish_point("update_ai_message")
#4、编译图
graph_app = graph_builder.compile()

#5、调用
print(graph_app.invoke({"messages":[("human", "你好你是？")]}))